<html>
<head><meta charset="utf-8"><title>Projection resolution documentation · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html">Projection resolution documentation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212535892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212535892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212535892">(Oct 07 2020 at 09:51)</a>:</h4>
<p>Hi,</p>
<p>I was wondering if there is any documentation (besides the code) on how projection chains are resolved in the rust code-gen? Has anyone written a blog post on that perhaps?</p>
<p>It seems like an involved topic.</p>
<p>Thanks</p>



<a name="212537084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212537084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212537084">(Oct 07 2020 at 10:04)</a>:</h4>
<p>Chains of <code>rustc::mir::ProjectionElem</code> or of <code>rustc::ty::Projection</code>? I think that the answer is no for both.</p>



<a name="212537607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212537607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212537607">(Oct 07 2020 at 10:09)</a>:</h4>
<p>The ones found in <code>Place</code>, so <code>ProjectionElem</code>.</p>



<a name="212545723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212545723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212545723">(Oct 07 2020 at 11:42)</a>:</h4>
<p>Have you seen <a href="#narrow/stream/131828-t-compiler/topic/codegen.20projections/near/210888831">https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/codegen.20projections/near/210888831</a>?</p>



<a name="212583997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212583997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212583997">(Oct 07 2020 at 16:26)</a>:</h4>
<p>Yeah, that's my colleague asking there :)</p>



<a name="212584349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212584349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212584349">(Oct 07 2020 at 16:28)</a>:</h4>
<p>I know. I was just curious if you saw it or missed it.</p>



<a name="212585819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212585819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212585819">(Oct 07 2020 at 16:40)</a>:</h4>
<p><span class="user-mention" data-user-id="223288">@Edd Barrett</span> In cg_clif a codegened place is represented as:</p>
<p><a href="https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/value_and_place.rs#L296-L309">https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/value_and_place.rs#L296-L309</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">struct</span> <span class="nc">CPlace</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">inner</span>: <span class="nc">CPlaceInner</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">layout</span>: <span class="nc">TyAndLayout</span><span class="o">&lt;'</span><span class="na">tcx</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="k">enum</span> <span class="nc">CPlaceInner</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Var</span><span class="p">(</span><span class="n">Local</span><span class="p">,</span><span class="w"> </span><span class="n">Variable</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">VarPair</span><span class="p">(</span><span class="n">Local</span><span class="p">,</span><span class="w"> </span><span class="n">Variable</span><span class="p">,</span><span class="w"> </span><span class="n">Variable</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">VarLane</span><span class="p">(</span><span class="n">Local</span><span class="p">,</span><span class="w"> </span><span class="n">Variable</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Addr</span><span class="p">(</span><span class="n">Pointer</span><span class="p">,</span><span class="w"> </span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">Value</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>The <code>Local</code> here is only used for debug printing of the <code>CPlace</code>. The <code>VarLane</code> variant is only used for SIMD vectors. For the <code>Addr</code> variant the <code>Pointer</code> is a combination of either an address or stackslot and a constant offset. This makes codegen a bit more efficient by folding multiple address calculations into one. The <code>Option&lt;Value&gt;</code> represents the metadata of a dereferenced pointer. For example a slice length or vtable pointer. <code>Variable</code> is converted to SSA values on the fly by cranelift-frontend.</p>
<p>The projection code is at <a href="https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/base.rs#L895-L965">https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/base.rs#L895-L965</a>. The called functions are at <a href="https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/value_and_place.rs#L668-L777">https://github.com/bjorn3/rustc_codegen_cranelift/blob/cb2dd4b5e05443a950dcd30d54a98c4c1c07a339/src/value_and_place.rs#L668-L777</a>.</p>
<p>I hope this helps.</p>



<a name="212586250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Projection%20resolution%20documentation/near/212586250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Projection.20resolution.20documentation.html#212586250">(Oct 07 2020 at 16:44)</a>:</h4>
<p>Thanks bjorn. I'll probably sit down and read the cranelift codegen tomorrow and see if we can take inspiration.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>